/*******************************************************************************
SightKit.h

	Author:		LiuDingXi
	e-mail:		liudx@gmail.com
	Date time:	2009-08-07

	Copyright (C) 2009
	All rights reserved.
	
*******************************************************************************/

#ifndef _SIGHTKIT_H_
#define _SIGHTKIT_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef GAME_BUILD
#include "DialogKitEx.h"
#else
#include "DialogKit.h"
#endif

#include "BaseParser.h"
#include "SpriteKit.h"
#include "MapKit.h"
#include "Coder.h"

#define SIGHT_EXT	_T(".dat")

class CSightKit : public CBaseParser
{
public:
	CSightKit();
	virtual ~CSightKit();

public:
	BOOL	Load(CArchive& ar);
	BOOL	Store(CArchive& ar);

	BOOL	Free();

	BOOL	SaveDat(LPCTSTR szFileName, int nFiles);
	SINT	SavePng(LPCTSTR szOutput);

	BOOL	Build(LPCTSTR szOutput, int iSight, CString& szMaps, CString& szSprites, CString& szDialogs);

public:

	struct SKIT : public CSpriteKit
	{
		CString szName;
		CString szFile;
		BOOL bExpand;
	};
	list<SKIT*> m_SprKits;

	struct DKIT : public CDialogKit
	{
		CString szName;
		CString szFile;
		BOOL bExpand;
	};

	list<DKIT*> m_DlgKits;

	struct MKIT : public CMapKit
	{
		CString szName;
		CString szFile;
		BOOL bExpand;
	};

	list<MKIT*> m_MapKits;


	typedef CSpriteKit::SPRITE SPRITE;
	typedef CDialogKit::DIALOG DIALOG;
	typedef CMapKit::TMAP TMAP;

	enum DTYPE{tGroup, tMap, tSprite, tDialog};
	struct TOBJECT
	{
		UINT nType;
		CString szName;

		int nPosX;
		int nPosY;

		union
		{
			LPVOID	pPoint;
			SPRITE* pSprite;
			DIALOG* pDialog;
			TMAP*	pMap;
		};
	};

	struct GROUP : public TOBJECT
	{
		list<TOBJECT*> ObjList;

		TOBJECT* AddObject(LPCTSTR p, UINT nType);
	};

	list<GROUP*>	m_Groups;

	CString m_szName;

	GROUP*	NewGroup(LPCTSTR p);

	BOOL	LinkObject(TOBJECT* pObject);

public:
	SPRITE* FindSprite(LPCTSTR szName);
	DIALOG* FindDialog(LPCTSTR szName);
	TMAP*	FindMap(LPCTSTR szName);


public:
	typedef CSpriteKit::IMAGE IMAGE;

	struct SIMG
	{
		IMAGE*	pImg;
		int		nMes;
		SIMG(IMAGE* pImage)
		{
			pImg = pImage;
			nMes = pImg->cx * pImg->cy;
		}
		bool operator < (const SIMG& c)const
		{
			return (nMes > c.nMes) ? true : false;
		}
	};
	struct STYLE
	{
		IMAGE*	pImg;
		int		nHash;
		STYLE(IMAGE* pImage)
		{
			pImg = pImage;
			nHash = 0;
			if(pImage)
			{
				const char* p = (const char*)&(pImage->style);
				int n = sizeof(pImage->style)/sizeof(char);
				for(int i=0; i<n; i++)
				{
					nHash = (nHash >> 13 ) | (nHash << 19);
					nHash += *p++;
				}
			}
		}
		bool operator < (const STYLE& c)const
		{
			return (nHash > c.nHash) ? true : false;
		}

	};

protected:
	int					m_nStyles;
	SceneDat::STYLE*	m_pStyles;

public:

	static SINT	SaveImages(list<SIMG>& ImgList, LPCTSTR szOutput);
	
	static BOOL	MakeImgAdd(list<IMAGE*>& PicList, IMAGE* pAdd, int xmax, int ymax);

	static inline bool IsCrossed(IMAGE* pImg1, IMAGE* pImg2);
	static inline bool IsCrossed(list<IMAGE*>& PicList, IMAGE* pTest);

	static BOOL	SavePng(LPCTSTR szFileName, list<IMAGE*>& PicList, int nImgId);
};


#endif//_SIGHTKIT_H_
